/**
 * Created by Tomkk on 2016/5/13.
 */
angular.module('app.service', [])
    .service('SystemService', function ($http) {
        var currentUser = {};
        var service = {
            getHostIp: function getHostIp() {
                //TODO 部署修改
                 return 'http://115.159.40.184:8080/club/';
                //return 'http://localhost:8080/';
            },
            transform: function transform(data) {
                return $.param(data);
            },
            clearSession: function () {
				//window.sessionStorage = null;
                window.sessionStorage.role = undefined;
				window.sessionStorage.email = undefined;
                window.sessionStorage.headimage = undefined;
                window.sessionStorage.mobile = undefined;
                window.sessionStorage.nickname = undefined;
                window.sessionStorage.user_id = undefined;
                window.sessionStorage.user_isuse = undefined;
                window.sessionStorage.username = undefined;
                window.sessionStorage.isLogin = undefined;
            },
            setCurrentUser: function (user) {
                //为了防止用户刷新丢失登陆用户信息，将用户信息存在session中
                window.sessionStorage.role = user.role;
                window.sessionStorage.email = user.email;
                window.sessionStorage.headimage = user.headimage;
                window.sessionStorage.mobile = user.mobile;
                window.sessionStorage.nickname = user.nickname;
                window.sessionStorage.realname = user.realname;
                window.sessionStorage.user_id = user.user_id;
                window.sessionStorage.user_isuse = user.user_isuse;
                window.sessionStorage.username = user.username;
            },
            getCurrentUser: function () {
                var user = {
                    role: window.sessionStorage.role,
                    email: window.sessionStorage.email,
                    headimage: window.sessionStorage.headimage,
                    mobile: window.sessionStorage.mobile,
                    nickname: window.sessionStorage.nickname,
                    realname: window.sessionStorage.realname,
                    user_id: window.sessionStorage.user_id,
                    user_isuse: window.sessionStorage.user_isuse,
                    username: window.sessionStorage.username
                }
                return user;
            }
        }
        return service;
    })
    .service('LoginService', function ($q, $http, SystemService) {
        var service = {
            login: login
        }
        return service;

        function login(user) {

            var deferred = $q.defer();
            $http.post(SystemService.getHostIp() + 'login',
                {username: user.username, password: user.password},
                {
                    headers: {'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'},
                    transformRequest: SystemService.transform
                })
                .success(function (data) {
                    //console.log(data);
                    deferred.resolve(data);
                })
                .error(function (data) {
                    deferred.reject(data);
                })
            return deferred.promise;
        }
    })
    .service('RegisterService', function ($q, $http, SystemService) {
        var service = {
            register: register,
            checkUsernameIsUsed: checkUsernameIsUsed
        }
        return service;

        function checkUsernameIsUsed(username) {
            var deferred = $q.defer();
            $http.post(SystemService.getHostIp() + 'checkUsername', {
                    username: username
                },
                {
                    headers: {'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'},
                    transformRequest: SystemService.transform
                })
                .success(function (data) {
                    deferred.resolve(data);
                })
                .error(function (data) {
                    deferred.reject(data);
                })
            return deferred.promise;
        }

        function register(user) {
            var deferred = $q.defer();
            $http.post(SystemService.getHostIp() + 'register', {
                    username: user.username,
                    password: user.password,
                    nickname: user.nickname,
                    realname: user.realname,
                    mobile: user.mobile,
                    email: user.email
                },
                {
                    headers: {'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'},
                    transformRequest: SystemService.transform
                })
                .success(function (data) {
                    deferred.resolve(data);
                })
                .error(function (data) {
                    deferred.reject(data);
                })
            return deferred.promise;
        }
    })
    .service('ManageUsersService', function ($q, $http, SystemService) {
        var users = [];
        var currentUser = {};
        var service = {
            deleteUser: function (user_id) {
                var deferred = $q.defer();
                $http.post(SystemService.getHostIp() + 'deleteUser', {
                        user_id: user_id
                    }
                    , {
                        headers: {'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'},
                        transformRequest: SystemService.transform
                    })
                    .success(function (data) {
                        deferred.resolve(data);
                    })
                    .error(function (data) {
                        deferred.reject(data);
                    })
                return deferred.promise;
            },
            getAllUsers: function () {
                var deferred = $q.defer();
                $http.get(SystemService.getHostIp() + 'getAllUsers')
                    .success(function (data) {
                        //console.log(data);
                        if (data.code == 0) {
                            users = data.returnValue;
                            deferred.resolve(data);
                        }
                    })
                    .error(function (data) {
                        deferred.reject(data);
                    })
                return deferred.promise;
            },
            setCurrentUser: function (user) {
                currentUser = user;
            },
            getCurrentUser: function () {
                return currentUser;
            },
            changeUserState: function (id, state) {
                var deferred = $q.defer();
                $http.post(SystemService.getHostIp() + 'changeUserState', {
                        user_id: id,
                        state: state
                    }
                    , {
                        headers: {'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'},
                        transformRequest: SystemService.transform
                    })
                    .success(function (data) {
                        deferred.resolve(data);
                    })
                    .error(function (data) {
                        deferred.reject(data);
                    })
                return deferred.promise;
            },
            switchUserRole: function (id, role) {
                var deferred = $q.defer();
                $http.post(SystemService.getHostIp() + 'changeUserRole', {
                        user_id: id,
                        role: role
                    }
                    , {
                        headers: {'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'},
                        transformRequest: SystemService.transform
                    })
                    .success(function (data) {
                        deferred.resolve(data);
                    })
                    .error(function (data) {
                        deferred.reject(data);
                    })
                return deferred.promise;
            },
            updateUser: function (user) {
                var deferred = $q.defer();
                $http.post(SystemService.getHostIp() + 'editUser', {
                        user_id: user.user_id,
                        nickname: user.nickname,
                        realname: user.realname,
                        mobile: user.mobile,
                        email: user.email,
                        role: user.role,
                        user_isuse: user.user_isuse
                    }
                    , {
                        headers: {'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'},
                        transformRequest: SystemService.transform
                    })
                    .success(function (data) {
                        deferred.resolve(data);
                    })
                    .error(function (data) {
                        deferred.reject(data);
                    })
                return deferred.promise;
            },
            updatePwd: function (id, password) {
                var deferred = $q.defer();
                $http.post(SystemService.getHostIp() + 'updatePwd', {
                        user_id: id,
                        password: password
                    }
                    , {
                        headers: {'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'},
                        transformRequest: SystemService.transform
                    })
                    .success(function (data) {
                        deferred.resolve(data);
                    })
                    .error(function (data) {
                        deferred.reject(data);
                    })
                return deferred.promise;
            },
            updateHeadImage: function (id, url) {
                var deferred = $q.defer();
                $http.post(SystemService.getHostIp() + 'updateHeadImage', {
                        user_id: id,
                        headimage: url
                    }
                    , {
                        headers: {'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'},
                        transformRequest: SystemService.transform
                    })
                    .success(function (data) {
                        deferred.resolve(data);
                    })
                    .error(function (data) {
                        deferred.reject(data);
                    })
                return deferred.promise;
            }
        }
        return service;
    })
    .service('AddNewsService', function ($q, $http, SystemService) {
        var service = {
            publish: function (title, content, id) {
                var deferred = $q.defer();
                $http.post(SystemService.getHostIp() + 'addNews', {
                        user_id: id,
                        news_title: title,
                        news_content: content
                    }
                    , {
                        headers: {'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'},
                        transformRequest: SystemService.transform
                    })
                    .success(function (data) {
                        deferred.resolve(data);
                    })
                    .error(function (data) {
                        deferred.reject(data);
                    })
                return deferred.promise;
            }
        }
        return service;
    })
    .service('ShowNewsService', function ($q, $http, SystemService) {
        var currentNews = {};
        var service = {
            setNews: function (id) {
                var deferred = $q.defer();
                $http.post(SystemService.getHostIp() + 'getNews', {
                        news_id: id
                    }
                    , {
                        headers: {'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'},
                        transformRequest: SystemService.transform
                    })
                    .success(function (data) {
                        deferred.resolve(data);
                        //console.log(data);
                        currentNews = data.returnValue;
                    })
                    .error(function (data) {
                        deferred.reject(data);
                    })
                return deferred.promise;
            },
            getNews: function () {
                return currentNews;
            },
            nextNews: function (id) {
                var deferred = $q.defer();
                $http.post(SystemService.getHostIp() + 'nextNews', {
                        news_id: id
                    }
                    , {
                        headers: {'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'},
                        transformRequest: SystemService.transform
                    })
                    .success(function (data) {
                        deferred.resolve(data);
                        //console.log(data);
                        currentNews = data.returnValue;
                    })
                    .error(function (data) {
                        deferred.reject(data);
                    })
                return deferred.promise;
            },
            preNews: function (id) {
                var deferred = $q.defer();
                $http.post(SystemService.getHostIp() + 'preNews', {
                        news_id: id
                    }
                    , {
                        headers: {'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'},
                        transformRequest: SystemService.transform
                    })
                    .success(function (data) {
                        deferred.resolve(data);
                        //console.log(data);
                        currentNews = data.returnValue;
                    })
                    .error(function (data) {
                        deferred.reject(data);
                    })
                return deferred.promise;
            },
            addComment: function (content) {
                var deferred = $q.defer();
                $http.post(SystemService.getHostIp() + 'addNewsComment', {
                        news_id: currentNews.news_id,
                        user_id: SystemService.getCurrentUser().user_id,
                        comment_content: content,
                        //user_id:1
                    }
                    , {
                        headers: {'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'},
                        transformRequest: SystemService.transform
                    })
                    .success(function (data) {
                        deferred.resolve(data);
                        //console.log(data);
                    })
                    .error(function (data) {
                        deferred.reject(data);
                    })
                return deferred.promise;
            },
            deleteComment: function (id) {
                var deferred = $q.defer();
                $http.post(SystemService.getHostIp() + 'deleteNewsComment', {
                        comment_id: id
                    }
                    , {
                        headers: {'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'},
                        transformRequest: SystemService.transform
                    })
                    .success(function (data) {
                        deferred.resolve(data);
                        //console.log(data);
                    })
                    .error(function (data) {
                        deferred.reject(data);
                    })
                return deferred.promise;
            }
        }
        return service;
    })
    .service('StartClubService', function ($q, $http, SystemService) {
        var service = {
            getAllNews: function () {
                var deferred = $q.defer();
                $http.get(SystemService.getHostIp() + 'getAllNews')
                    .success(function (data) {
                        //console.log(data);
                        if (data.code == 0) {
                            deferred.resolve(data);
                        }
                    })
                    .error(function (data) {
                        deferred.reject(data);
                    })
                return deferred.promise;
            },
            getAllAchievement: function () {
                var deferred = $q.defer();
                $http.get(SystemService.getHostIp() + 'getAllAchievement')
                    .success(function (data) {
                        //console.log(data);
                        if (data.code == 0) {
                            deferred.resolve(data);
                        }
                    })
                    .error(function (data) {
                        deferred.reject(data);
                    })
                return deferred.promise;
            },
            getAllMyNews: function (user_id) {
                var deferred = $q.defer();
                $http.post(SystemService.getHostIp() + 'getAllMyNews', {
                        user_id: user_id
                    }
                    , {
                        headers: {'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'},
                        transformRequest: SystemService.transform
                    })
                    .success(function (data) {
                        deferred.resolve(data);
                        //console.log(data);
                    })
                    .error(function (data) {
                        deferred.reject(data);
                    })
                return deferred.promise;
            },
            getAllMyAchievement: function (user_id) {
                var deferred = $q.defer();
                $http.post(SystemService.getHostIp() + 'getAllMyAchievement', {
                        user_id: user_id
                    }
                    , {
                        headers: {'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'},
                        transformRequest: SystemService.transform
                    })
                    .success(function (data) {
                        deferred.resolve(data);
                        //console.log(data);
                    })
                    .error(function (data) {
                        deferred.reject(data);
                    })
                return deferred.promise;
            },
            deleteNews: function (news_id) {
                var deferred = $q.defer();
                $http.post(SystemService.getHostIp() + 'deleteNews', {
                        news_id: news_id
                    }
                    , {
                        headers: {'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'},
                        transformRequest: SystemService.transform
                    })
                    .success(function (data) {
                        deferred.resolve(data);
                        //console.log(data);
                    })
                    .error(function (data) {
                        deferred.reject(data);
                    })
                return deferred.promise;
            },
            deleteAchievement: function (achievement_id) {
                var deferred = $q.defer();
                $http.post(SystemService.getHostIp() + 'deleteAchievement', {
                        achievement_id: achievement_id
                    }
                    , {
                        headers: {'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'},
                        transformRequest: SystemService.transform
                    })
                    .success(function (data) {
                        deferred.resolve(data);
                        //console.log(data);
                    })
                    .error(function (data) {
                        deferred.reject(data);
                    })
                return deferred.promise;
            }
        }
        return service;
    })
    .service('EditNewsService', function ($q, $http, SystemService) {
        var service = {
            update: function (news_title, news_content, news_id) {
                var deferred = $q.defer();
                $http.post(SystemService.getHostIp() + 'editNews', {
                        news_title: news_title,
                        news_content: news_content,
                        news_id: news_id
                    }
                    , {
                        headers: {'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'},
                        transformRequest: SystemService.transform
                    })
                    .success(function (data) {
                        deferred.resolve(data);
                        //console.log(data);
                    })
                    .error(function (data) {
                        deferred.reject(data);
                    })
                return deferred.promise;
            }
        }
        return service;
    })
    .service('AddAchievementService', function ($q, $http, SystemService) {
        var service = {
            publish: function (title, content, id) {
                var deferred = $q.defer();
                $http.post(SystemService.getHostIp() + 'addAchievement', {
                        user_id: id,
                        achievement_title: title,
                        achievement_content: content
                    }
                    , {
                        headers: {'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'},
                        transformRequest: SystemService.transform
                    })
                    .success(function (data) {
                        deferred.resolve(data);
                    })
                    .error(function (data) {
                        deferred.reject(data);
                    })
                return deferred.promise;
            }
        }
        return service;
    })
    .service('ShowAchievementService', function ($q, $http, SystemService) {
        var currentAchievement = {};
        var service = {
            setAchievement: function (id) {
                var deferred = $q.defer();
                $http.post(SystemService.getHostIp() + 'getAchievement', {
                        achievement_id: id
                    }
                    , {
                        headers: {'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'},
                        transformRequest: SystemService.transform
                    })
                    .success(function (data) {
                        deferred.resolve(data);
                        //console.log(data);
                        currentAchievement = data.returnValue;
                    })
                    .error(function (data) {
                        deferred.reject(data);
                    })
                return deferred.promise;
            },
            getAchievement: function () {
                return currentAchievement;
            },
            nextAchievement: function (id) {
                var deferred = $q.defer();
                $http.post(SystemService.getHostIp() + 'nextAchievement', {
                        achievement_id: id
                    }
                    , {
                        headers: {'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'},
                        transformRequest: SystemService.transform
                    })
                    .success(function (data) {
                        deferred.resolve(data);
                        //console.log(data);
                        currentAchievement = data.returnValue;
                    })
                    .error(function (data) {
                        deferred.reject(data);
                    })
                return deferred.promise;
            },
            preAchievement: function (id) {
                var deferred = $q.defer();
                $http.post(SystemService.getHostIp() + 'preAchievement', {
                        achievement_id: id
                    }
                    , {
                        headers: {'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'},
                        transformRequest: SystemService.transform
                    })
                    .success(function (data) {
                        deferred.resolve(data);
                        //console.log(data);
                        currentAchievement = data.returnValue;
                    })
                    .error(function (data) {
                        deferred.reject(data);
                    })
                return deferred.promise;
            },
            addComment: function (content) {
                var deferred = $q.defer();
                $http.post(SystemService.getHostIp() + 'addAchievementComment', {
                        achievement_id: currentAchievement.achievement_id,
                        //TODO 写死的，需要修改
                        user_id: SystemService.getCurrentUser().user_id,
                        comment_content: content,
                        // user_id:1
                    }
                    , {
                        headers: {'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'},
                        transformRequest: SystemService.transform
                    })
                    .success(function (data) {
                        deferred.resolve(data);
                        //console.log(data);
                    })
                    .error(function (data) {
                        deferred.reject(data);
                    })
                return deferred.promise;
            },
            deleteComment: function (id) {
                var deferred = $q.defer();
                $http.post(SystemService.getHostIp() + 'deleteAchievementComment', {
                        comment_id: id
                    }
                    , {
                        headers: {'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'},
                        transformRequest: SystemService.transform
                    })
                    .success(function (data) {
                        deferred.resolve(data);
                        //console.log(data);
                    })
                    .error(function (data) {
                        deferred.reject(data);
                    })
                return deferred.promise;
            }
        }
        return service;
    })
    .service('EditAchievementService', function ($q, $http, SystemService) {
        var service = {
            update: function (achievement_title, achievement_content, achievement_id) {
                var deferred = $q.defer();
                $http.post(SystemService.getHostIp() + 'editAchievement', {
                        achievement_title: achievement_title,
                        achievement_content: achievement_content,
                        achievement_id: achievement_id
                    }
                    , {
                        headers: {'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'},
                        transformRequest: SystemService.transform
                    })
                    .success(function (data) {
                        deferred.resolve(data);
                        //console.log(data);
                    })
                    .error(function (data) {
                        deferred.reject(data);
                    })
                return deferred.promise;
            }
        }
        return service;
    })
    .service('ShowClubMembersService', function (SystemService, $http, $q) {
        var service = {
            getAllClubMembers: function () {
                var deferred = $q.defer();
                $http.get(SystemService.getHostIp() + 'getAllClubMembers')
                    .success(function (data) {
                        deferred.resolve(data);
                        //console.log(data);
                    })
                    .error(function (data) {
                        deferred.reject(data);
                    })
                return deferred.promise;
            }
        }
        return service;
    })
